package saxion.speelveld.spaceengineer.model;

import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.controller.BitmapManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

public class InfrastructureTile extends Tile
{
	private boolean connectedToSpaceCenter;

	private boolean connectedRight, connectedLeft, connectedTop, connectedBottom;

	public InfrastructureTile(int backgroundID)
	{
		super(backgroundID);
		connectedToSpaceCenter = false;
		connectedLeft = false;
		connectedRight = false;
		connectedBottom = false;
		connectedTop = false;
	}

	public Bitmap getPic(Context context, int tileSize)
	{
		if (connectedRight)
		{
			if (connectedLeft)
			{
				if (connectedTop)
				{
					if (connectedBottom)
					{
						return BitmapManager.getScaledBitmapFromResources(R.drawable.glasscrossroad, context, tileSize);
					}
					else
					{
						return BitmapManager.getScaledBitmapFromResources(R.drawable.glasstoptoleftandright, context,
							tileSize);
					}
				}
				else
				{
					if (connectedBottom)
					{
						return BitmapManager.getScaledBitmapFromResources(R.drawable.glassbottomtoleftandright,
							context, tileSize);
					}
					else
					{
						return BitmapManager
							.getScaledBitmapFromResources(R.drawable.glasshorizontal, context, tileSize);
					}
				}
			}
			else
			{
				if (connectedTop)
				{
					if (connectedBottom)
					{
						return BitmapManager.getScaledBitmapFromResources(R.drawable.glassrighttotopandbottom, context,
							tileSize);
					}
					else
					{
						return BitmapManager.getScaledBitmapFromResources(R.drawable.glasscornertoptoright, context,
							tileSize);
					}
				}
				else
				{
					if (connectedBottom)
					{
						return BitmapManager.getScaledBitmapFromResources(R.drawable.glasscornerbottomtoright, context,
							tileSize);
					}
					else
					{
						return BitmapManager.getScaledBitmapFromResources(R.drawable.glassonlyright, context, tileSize);
					}
				}
			}
		}
		else if (connectedLeft)
		{
			if (connectedTop)
			{
				if (connectedBottom)
				{
					return BitmapManager.getScaledBitmapFromResources(R.drawable.glasslefttotopandbottom, context,
						tileSize);
				}
				else
				{
					return BitmapManager.getScaledBitmapFromResources(R.drawable.glasscornertoptoleft, context,
						tileSize);
				}
			}
			else
			{
				if (connectedBottom)
				{
					return BitmapManager.getScaledBitmapFromResources(R.drawable.glasscornerbottomtoleft, context,
						tileSize);
				}
				else
				{
					return BitmapManager.getScaledBitmapFromResources(R.drawable.glassonlyleft, context, tileSize);
				}
			}
		}
		else if (connectedTop)
		{
			if (connectedBottom)
			{
				return BitmapManager.getScaledBitmapFromResources(R.drawable.glassvertical, context, tileSize);
			}
			else
			{
				return BitmapManager.getScaledBitmapFromResources(R.drawable.glassonlytop, context, tileSize);
			}
		}
		else if (connectedBottom)
		{
			return BitmapManager.getScaledBitmapFromResources(R.drawable.glassonlybottom, context, tileSize);
		}
		else
		{
			return BitmapManager.getScaledBitmapFromResources(R.drawable.glassnotconnected, context, tileSize);
		}
	}

	public void setConnectionVarsFromString(String input)
	{
		char[] charArray = input.toCharArray();
		boolean connection = false;
		for (int i = 0; i < charArray.length; i++)
		{
			connection = false;
			int iConnection = Character.getNumericValue(charArray[i]);
			Log.i("setConnectionVarsFromString", iConnection + "");
			if (iConnection == 1)
			{
				connection = true;
			}
			switch (i)
			{
			case 0:
				connectedLeft = connection;
				break;
			case 1:
				connectedTop = connection;
				break;
			case 2:
				connectedRight = connection;
				break;
			case 3:
				connectedBottom = connection;
				break;
			}
		}
	}

	public void setConnected(boolean connected)
	{
		connectedToSpaceCenter = connected;
	}

	public boolean isConnected()
	{
		return connectedToSpaceCenter;
	}

	public boolean isConnectedRight()
	{
		return connectedRight;
	}

	public void setConnectedRight(boolean connectedRight)
	{
		this.connectedRight = connectedRight;
		this.setDirty(true);
	}

	public boolean isConnectedLeft()
	{
		return connectedLeft;
	}

	public void setConnectedLeft(boolean connectedLeft)
	{
		this.connectedLeft = connectedLeft;
		this.setDirty(true);
	}

	public boolean isConnectedTop()
	{
		return connectedTop;
	}

	public void setConnectedTop(boolean connectedTop)
	{
		this.connectedTop = connectedTop;
		this.setDirty(true);
	}

	public boolean isConnectedBottom()
	{
		return connectedBottom;
	}

	public void setConnectedBottom(boolean connectedBottom)
	{
		this.connectedBottom = connectedBottom;
		this.setDirty(true);
	}
}
